package com.www.applyword.fragment;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.allen.library.SuperTextView;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.base.BaseFragment;
import com.www.applyword.bean.LoginBean;
import com.www.applyword.event.EventConstants;
import com.www.applyword.event.MessageEvent;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.ui.home.PostJobActivity;
import com.www.applyword.ui.my.CodeActivity;
import com.www.applyword.ui.my.MyBrokerageActivity;
import com.www.applyword.ui.my.MyEnshrineActivity;
import com.www.applyword.ui.my.MyEnterpriseActivity;
import com.www.applyword.ui.my.MyEpositActivity;
import com.www.applyword.ui.my.MyJobActivity;
import com.www.applyword.ui.my.MyJobTimeActivity;
import com.www.applyword.ui.my.MyReSumeActivity;
import com.www.applyword.ui.my.MyRealNameActivity;
import com.www.applyword.ui.my.MyUserInfoActivity;
import com.www.applyword.ui.my.OpintionActivity;
import com.www.applyword.ui.my.SettingActivity;
import com.www.applyword.utils.ImageUtil;
import com.www.applyword.utils.SPUtil;
import com.www.applyword.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2019/10/18 0018 15:55
 * version: 1.0
 *
 * @author Administrator
 */
public class MyFragment extends BaseFragment {
    @BindView(R.id.tv_my_photo)
    ImageView tvMyPhoto;
    @BindView(R.id.tv_my_name)
    TextView tvMyName;
    @BindView(R.id.tv_my_phone)
    TextView tv_my_phone;
    @BindView(R.id.tv_real_name)
    TextView tvRealName;
    @BindView(R.id.sv_my_brokerage)
    SuperTextView svMyBrokerage;
    @BindView(R.id.sv_my_eposit)
    SuperTextView svMyEposit;
    @BindView(R.id.ll_have_launched)
    LinearLayout llHaveLaunched;
    @BindView(R.id.ll_applied)
    LinearLayout llApplied;
    @BindView(R.id.ll_has_contracted)
    LinearLayout llHasContracted;
    @BindView(R.id.ll_has_cleared)
    LinearLayout llHasCleared;
    @BindView(R.id.ll_registered)
    LinearLayout llRegistered;
    @BindView(R.id.ll_have_applied)
    LinearLayout llHaveApplied;
    @BindView(R.id.ll_post)
    LinearLayout llPost;
    @BindView(R.id.ll_cleared)
    LinearLayout llCleared;
    @BindView(R.id.sv_enshrine)
    SuperTextView svEnshrine;
    @BindView(R.id.sv_invitation)
    SuperTextView svInvitation;
    @BindView(R.id.sv_release)
    SuperTextView svRelease;
    @BindView(R.id.sv_contact)
    SuperTextView svContact;
    @BindView(R.id.sv_setting)
    SuperTextView svSetting;
    private int userInfo_state;
    private String customerServicePhone;
    private String userInfo_headImg;

    @Override
    protected int getLayoutResource() {
        return R.layout.my_fragment;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(EventConstants.PAY) ) {
                initView();
            }else if (messageEvent.getMessageKey().equals(EventConstants.REAL_NAME)){
                initView();
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {

        } else {
            int state =  SPUtil.getInt("userInfo_state", 0);
            if (state != 2)initView();
        }
    }

    private void initView() {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", SPUtil.getString(Constants.USERID));
        HttpClient.get(getActivity(), Constants.GETUSERINFO, params,new CallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean result) {
                customerServicePhone = result.CustomerServicePhone;
                userInfo_headImg = result.getUser().getUserInfo_HeadImg();
                SPUtil.put(Constants.NAME,result.getUser().getUserInfo_NickName());
                if (!TextUtils.isEmpty(userInfo_headImg))
                SPUtil.put(Constants.PHOTO,result.getUser().getUserInfo_HeadImg());
                SPUtil.put(Constants.JIFFEN,result.getUser().getUserInfo_Integer());
                ImageUtil.setImage(tvMyPhoto,result.getUser().getUserInfo_HeadImg());
                tvMyName.setText(result.getUser().getUserInfo_NickName());
                tv_my_phone.setText(result.getUser().getUserInfo_Mobile());
                svMyBrokerage.getLeftTopTextView().setText(result.getUser().getUserInfo_Money() + "");
                //我的保证金
                svMyBrokerage.getRightTopTextView().setText(result.getUser().getUserInfo_TotalMoney() + "");
                userInfo_state = result.getUser().getUserInfo_State();
                SPUtil.put("userInfo_state",userInfo_state);
                if (result.getUser().getUserInfo_State() == 2){  //UserInfo_State 0 正常(未提交实名认证信息) -1待审核 1禁用 2实名认证
                    tvRealName.setText("已实名认证");
                    tvRealName.setClickable(false);
                  //  Utils.showToastSafe("已实名认证");
                }else if (result.getUser().getUserInfo_State() == -1){
                    tvRealName.setText("待审核");
                    tvRealName.setClickable(false);
                 //   Utils.showToastSafe("待审核");
                }else if (result.getUser().getUserInfo_State() == 0){
                    tvRealName.setText("实名认证");
                }else {
                    tvRealName.setText("禁用");
                    tvRealName.setClickable(false);
                }
            }
        });

        svMyBrokerage.getLeftTopTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyBrokerageActivity.class).putExtra("money", svMyBrokerage.getLeftTopString()));
            }
        });
        svMyBrokerage.getLeftBottomTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyBrokerageActivity.class).putExtra("money", svMyBrokerage.getLeftTopString()));
            }
        });

        svMyBrokerage.getRightTopTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyEpositActivity.class).putExtra("money", svMyBrokerage.getRightTopString()));
            }
        });
        svMyBrokerage.getRightBottomTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyEpositActivity.class).putExtra("money", svMyBrokerage.getRightTopString()));
            }
        });
    }

    @OnClick({R.id.ll_have_launched, R.id.ll_applied, R.id.ll_has_contracted, R.id.ll_has_cleared,
            R.id.ll_registered, R.id.ll_have_applied, R.id.ll_post, R.id.ll_cleared, R.id.sv_enshrine,
            R.id.sv_invitation, R.id.sv_release, R.id.sv_contact, R.id.sv_setting,R.id.tv_my_photo,
            R.id.sv_my_brokerage,R.id.sv_my_eposit,R.id.sv_resume,R.id.sv_enterprise,R.id.tv_real_name,R.id.ll_user,R.id.tv_opinion})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //已发布
            case R.id.ll_have_launched:
                startActivity(new Intent(getActivity(), MyJobActivity.class).putExtra("type", 1));
                break;
            case R.id.ll_applied:
                startActivity(new Intent(getActivity(), MyJobActivity.class).putExtra("type", 2));
                break;
            case R.id.ll_has_contracted:
                startActivity(new Intent(getActivity(), MyJobActivity.class).putExtra("type", 3));
                break;
            case R.id.ll_has_cleared:
                startActivity(new Intent(getActivity(), MyJobActivity.class).putExtra("type", 4));
                break;
                //已报名
            case R.id.ll_registered:
                startActivity(new Intent(getActivity(), MyJobTimeActivity.class).putExtra("type", 1));
                break;
                //已录用
            case R.id.ll_have_applied:
                startActivity(new Intent(getActivity(), MyJobTimeActivity.class).putExtra("type", 2));
                break;
                //已到岗
            case R.id.ll_post:
                startActivity(new Intent(getActivity(), MyJobTimeActivity.class).putExtra("type", 3));
                break;
                //已结算
            case R.id.ll_cleared:
                startActivity(new Intent(getActivity(), MyJobTimeActivity.class).putExtra("type", 4));
                break;
                //收藏
            case R.id.sv_enshrine:
                startActivity(new Intent(getActivity(), MyEnshrineActivity.class));
                break;
            case R.id.ll_user:
                startActivity(new Intent(getActivity(), MyUserInfoActivity.class).putExtra("url", userInfo_headImg));
                break;
                //我的邀请
            case R.id.sv_invitation:
                Utils.startActivity(CodeActivity.class);
                break;
                //我要发布
            case R.id.sv_release:
                if (userInfo_state == 0){
                    Utils.showToastSafe("请先实名认证");
                    Utils.startActivity(MyRealNameActivity.class);
                    return;
                }
                if (userInfo_state == -1){
                    Utils.showToastSafe("您的实名认证正在审核中");
                    return;
                }
                startActivity(new Intent(getActivity(), PostJobActivity.class));
                break;
            case R.id.sv_contact:
                showContact();
                break;
            case R.id.sv_setting:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.sv_my_brokerage:
             //   startActivity(new Intent(getActivity(), MyBrokerageActivity.class).putExtra("money", svMyBrokerage.getCenterTopString()));
                break;
            case R.id.sv_my_eposit:
                startActivity(new Intent(getActivity(), MyEpositActivity.class).putExtra("money", svMyEposit.getCenterTopString()));
                break;
            case R.id.sv_resume:
                if (userInfo_state == 0){
                    Utils.showToastSafe("请先实名认证");
                    Utils.startActivity(MyRealNameActivity.class);
                    return;
                }
                if (userInfo_state == -1){
                    Utils.showToastSafe("您的实名认证正在审核中");
                    return;
                }
                startActivity(new Intent(getActivity(), MyReSumeActivity.class));
                break;
                //企业认证
            case R.id.sv_enterprise:
                startActivity(new Intent(getActivity(), MyEnterpriseActivity.class));
                break;
                //实名认证
            case R.id.tv_real_name:
                startActivity(new Intent(getActivity(), MyRealNameActivity.class));
                break;
                //头像
            case R.id.tv_my_photo:
              //  startActivity(new Intent(getActivity(), MyUserInfoActivity.class));
                startActivity(new Intent(getActivity(), MyUserInfoActivity.class).putExtra("url", userInfo_headImg));
                break;
            case R.id.tv_opinion:
              //  startActivity(new Intent(getActivity(), MyUserInfoActivity.class));
                Utils.startActivity(OpintionActivity.class);
                break;
                default:
        }
    }

    private void showContact() {
        Dialog  dialogHead = new Dialog(getActivity(), R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_head_portrait2, null);
        Button phone  = view.findViewById(R.id.dialog_photo_album);
        Button dialog_cancel  = view.findViewById(R.id.dialog_cancel);
        phone.setText(customerServicePhone);
        dialog_cancel.setOnClickListener(v -> dialogHead.dismiss());
        phone.setOnClickListener(v -> {
            RxPermissions rxPermissions = new RxPermissions(getActivity());
            rxPermissions
                    .request(Manifest.permission.CALL_PHONE)
                    .subscribe(granted -> {
                        if (granted) {
                            Intent intent = new Intent(Intent.ACTION_DIAL);
                            Uri data = Uri.parse("tel:" + customerServicePhone);
                            intent.setData(data);
                            startActivity(intent);
                            //  Toast.makeText(MainActivity.this, "同意权限", Toast.LENGTH_SHORT).show();
                        } else {
                            //  Toast.makeText(MainActivity.this, "拒绝权限", Toast.LENGTH_SHORT).show();
                        }
                    });
        });

        dialogHead.setContentView(view);
        Window window = dialogHead.getWindow();
        if (window == null) {
            return;
        }
        dialogHead.show();//显示对话框//设置Dialog从窗体底部弹出
        window.setGravity(Gravity.BOTTOM);
        //获取窗体的属性

        //将属性设置给窗体
    //    window.setAttributes(lp);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.y = 20;//设置Dialog距离底部的距离
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

        window.setAttributes(lp);
    }
}
